package com.adinnet.api.order.controller;


import com.adinnet.api.order.model.vo.OrderDetailsResp;
import com.adinnet.api.order.model.vo.OrderReq;
import com.adinnet.api.order.model.vo.OrderResp;
import com.adinnet.api.order.service.OrderService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResp;
import com.adinnet.core.exception.BizException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 兑换
     *
     * @param orderReq
     * @param request
     * @return
     */
    @PostMapping("/create")
    public JsonResult createOrder(@Valid @RequestBody OrderReq orderReq, HttpServletRequest request) {
        try {

            return orderService.createOrder(orderReq, request);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("兑换失败");
        }
    }

    /**
     * 订单列表   一个家长可能有多个儿童，所以需要传儿童id
     *
     * @return
     */
    @GetMapping("list")
    public PageResp orderList(Integer childId, Integer page, Integer limit, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (null == childId) {
                return new PageResp("childId不能为空");
            }
            if (null == page) {
                return new PageResp("page不能为空");
            }
            if (null == limit) {
                return new PageResp("limit不能为空");
            }
            Page<OrderResp> orderList = orderService.orderList(childId, page, limit);
            if (null == orderList || CollectionUtils.isEmpty(orderList.getRecords())) {
                return new PageResp(Lists.newArrayList());
            }
            return new PageResp(orderList.getRecords(), orderList.getTotal());
        } catch (BizException e) {
            return new PageResp(e.getMessage());
        } catch (Exception e) {
            return new PageResp("查询订单列表失败!");
        }
    }

    /**
     * 订单详情
     *
     * @return
     */
    @GetMapping("list/details")
    public JsonResult orderDetails(String orderNo, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (StringUtils.isEmpty(orderNo)) {
                return JsonResult.error("orderNo不能为空");
            }
            OrderDetailsResp orderDetails = orderService.orderDetails(orderNo);
            if (null == orderDetails) {
                return JsonResult.ok().put("data", new JSONObject());
            }
            return JsonResult.ok().put("data", orderDetails);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("查询订单详情失败!");
        }
    }

}
